from flask import Flask, render_template, request
from flask_cors import CORS
import pymysql.constants
from collections import Counter
import re

connection = pymysql.connect(host='localhost',
                             user='root',
                             password='123456',
                             database='soft',
                             cursorclass=pymysql.cursors.DictCursor)
cursor = connection.cursor()
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})


#网络安全
@app.route('/getwangluoanquanData')
def getwangluoanquanData():
    sql = "select * from salary where type=1"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()
    return result

@app.route("/getwangluoanquanEcharts")
def getwangluoanquanEcharts():
    sql = "select skills from salary where type=1"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()
    infos = []
    for item in result:
        infos += (item["skills"].replace(" ", "").split("\n"))

    infos = (Counter(infos))
    infos = infos.most_common()
    print(infos)
    X = []
    Y = []
    for item in infos:
        if item[0]:
            X.append(item[0])
            Y.append(item[1])

    return [X[:10], Y[:10]]

@app.route("/getwangluoanquanEduEcharts")
def getwangluoanquanEduEcharts():
    sql = "select education from salary where type=1"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()
    infos = []
    for item in result:
        if item["education"].replace(" ", "").replace("\n", ""):
            infos.append(item["education"].replace(" ", "").replace("\n", ""))
    infos = (Counter(infos).most_common())
    arr = []
    for item in infos:
        obj = {}
        obj["value"] = item[1]
        obj["name"] = item[0]
        arr.append(obj)
    return arr[:10]

@app.route("/getwangluoanquanEduAndSalaryEcharts")
def getwangluoanquanEduAndSalaryEcharts():
    sql = "select education,salary from salary where type=1"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()

    total = {}
    totalnum = {}
    for item in result:
        if item["education"]:
            edu = item["education"].replace(" ", "").replace("\n", "")
            price = item["salary"].replace(" ", "").replace("\n", "")

            if not (re.findall("天", price)) and re.findall("(千|万)", price):
                nums = re.findall("\d+\.{0,1}\d*", price)
                flag = re.findall("(千|万)", price)
                num1 = float(nums[0]) * 1000 if flag[0] == "千" else float(nums[0]) * 10000
                num2 = float(nums[1]) * 1000 if flag[1] == "千" else float(nums[1]) * 10000
                avg = (num1 + num2) / 2
                if total.get(edu):
                    total[edu] += avg
                    totalnum[edu] += 1
                else:
                    total[edu] = avg
                    totalnum[edu] = 1

    X = []
    Y = []
    for item in total:
        X.append(item)
        Y.append(total[item] / totalnum[item])

    print(X)
    print(Y)
    return [X[:10], Y[:10]]

@app.route("/getwangluoanquanScore")
def getwangluoanquanScore():
    sql = "select skills from salary where type=1"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()

    infos = []
    for item in result:
        infos += (item["skills"].replace(" ", "").split("\n"))

    infos = (Counter(infos))
    infos = infos.most_common()
    total = 0
    newinfos = []
    for item in infos:
        if item[0]:
            total += item[1]
            newinfos.append(list(item))
    for item in newinfos:
        item[1] = item[1] / total
    return newinfos[:10]



#人工智能
@app.route('/getrengongzhinengData')
def getrengongzhinengData():
    sql = "select * from salary where type=2"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()
    return result

@app.route("/getrgznEcharts")
def getrgznEcharts():
    sql = "select skills from salary where type=2"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()
    infos = []
    for item in result:
        infos += (item["skills"].replace(" ", "").split("\n"))

    infos = (Counter(infos))
    infos = infos.most_common()
    print(infos)
    X = []
    Y = []
    for item in infos:
        if item[0]:
            X.append(item[0])
            Y.append(item[1])

    return [X[:10], Y[:10]]

@app.route("/getrgznEduEcharts")
def getrgznEduEcharts():
    sql = "select education from salary where type=2"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()
    infos = []
    for item in result:
        if item["education"].replace(" ", "").replace("\n", ""):
            infos.append(item["education"].replace(" ", "").replace("\n", ""))
    infos = (Counter(infos).most_common())
    arr = []
    for item in infos:
        obj = {}
        obj["value"] = item[1]
        obj["name"] = item[0]
        arr.append(obj)
    return arr[:10]

@app.route("/getrgznEduAndSalaryEcharts")
def getrgznEduAndSalaryEcharts():
    sql = "select education,salary from salary where type=2"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()

    total = {}
    totalnum = {}
    for item in result:
        if item["education"]:
            edu = item["education"].replace(" ", "").replace("\n", "")
            price = item["salary"].replace(" ", "").replace("\n", "")

            if not (re.findall("天", price)) and re.findall("(千|万)", price):
                nums = re.findall("\d+\.{0,1}\d*", price)
                flag = re.findall("(千|万)", price)
                num1 = float(nums[0]) * 1000 if flag[0] == "千" else float(nums[0]) * 10000
                num2 = float(nums[1]) * 1000 if flag[1] == "千" else float(nums[1]) * 10000
                avg = (num1 + num2) / 2
                if total.get(edu):
                    total[edu] += avg
                    totalnum[edu] += 1
                else:
                    total[edu] = avg
                    totalnum[edu] = 1

    X = []
    Y = []
    for item in total:
        X.append(item)
        Y.append(total[item] / totalnum[item])

    print(X)
    print(Y)
    return [X[:10], Y[:10]]

@app.route("/getrgznScore")
def getrgznScore():
    sql = "select skills from salary where type=2"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()

    infos = []
    for item in result:
        infos += (item["skills"].replace(" ", "").split("\n"))

    infos = (Counter(infos))
    infos = infos.most_common()
    total = 0
    newinfos = []
    for item in infos:
        if item[0]:
            total += item[1]
            newinfos.append(list(item))
    for item in newinfos:
        item[1] = item[1] / total
    return newinfos[:10]


#软件开发
@app.route('/getruanjiankaifaData')
def getruanjiankaifaData():
    sql = "select * from salary where type=3"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()
    return result

@app.route("/getkfEcharts")
def getkfEcharts():
    sql = "select skills from salary where type=3"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()
    infos = []
    for item in result:
        infos += (item["skills"].replace(" ", "").split("\n"))

    infos = (Counter(infos))
    infos = infos.most_common()
    print(infos)
    X = []
    Y = []
    for item in infos:
        if item[0]:
            X.append(item[0])
            Y.append(item[1])

    return [X[:10], Y[:10]]

@app.route("/getkfEduEcharts")
def getkfEduEcharts():
    sql = "select education from salary where type=3"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()
    infos = []
    for item in result:
        if item["education"].replace(" ", "").replace("\n", ""):
            infos.append(item["education"].replace(" ", "").replace("\n", ""))
    infos = (Counter(infos).most_common())
    arr = []
    for item in infos:
        obj = {}
        obj["value"] = item[1]
        obj["name"] = item[0]
        arr.append(obj)
    return arr[:10]

@app.route("/getkfEduAndSalaryEcharts")
def getkfEduAndSalaryEcharts():
    sql = "select education,salary from salary where type=3"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()

    total = {}
    totalnum = {}
    for item in result:
        if item["education"]:
            edu = item["education"].replace(" ", "").replace("\n", "")
            price = item["salary"].replace(" ", "").replace("\n", "")

            if not (re.findall("天", price)) and re.findall("(千|万)", price):
                nums = re.findall("\d+\.{0,1}\d*", price)
                flag = re.findall("(千|万)", price)
                num1 = float(nums[0]) * 1000 if flag[0] == "千" else float(nums[0]) * 10000
                num2 = float(nums[1]) * 1000 if flag[1] == "千" else float(nums[1]) * 10000
                avg = (num1 + num2) / 2
                if total.get(edu):
                    total[edu] += avg
                    totalnum[edu] += 1
                else:
                    total[edu] = avg
                    totalnum[edu] = 1

    X = []
    Y = []
    for item in total:
        X.append(item)
        Y.append(total[item] / totalnum[item])

    print(X)
    print(Y)
    return [X[:10], Y[:10]]

@app.route("/getkfScore")
def getkfScore():
    sql = "select skills from salary where type=3"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()

    infos = []
    for item in result:
        infos += (item["skills"].replace(" ", "").split("\n"))

    infos = (Counter(infos))
    infos = infos.most_common()
    total = 0
    newinfos = []
    for item in infos:
        if item[0]:
            total += item[1]
            newinfos.append(list(item))
    for item in newinfos:
        item[1] = item[1] / total
    return newinfos[:10]


#测试工程师
@app.route('/getceshiData')
def getceshiData():
    sql = "select * from salary where type=4"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()
    return result

@app.route("/getcsEcharts")
def getcsEcharts():
    sql = "select skills from salary where type=4"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()
    infos = []
    for item in result:
        infos += (item["skills"].replace(" ", "").split("\n"))

    infos = (Counter(infos))
    infos = infos.most_common()
    print(infos)
    X = []
    Y = []
    for item in infos:
        if item[0]:
            X.append(item[0])
            Y.append(item[1])

    return [X[:10], Y[:10]]

@app.route("/getcsEduEcharts")
def getcsEduEcharts():
    sql = "select education from salary where type=4"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()
    infos = []
    for item in result:
        if item["education"].replace(" ", "").replace("\n", ""):
            infos.append(item["education"].replace(" ", "").replace("\n", ""))
    infos = (Counter(infos).most_common())
    arr = []
    for item in infos:
        obj = {}
        obj["value"] = item[1]
        obj["name"] = item[0]
        arr.append(obj)
    return arr[:10]

@app.route("/getcsEduAndSalaryEcharts")
def getcsEduAndSalaryEcharts():
    sql = "select education,salary from salary where type=4"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()

    total = {}
    totalnum = {}
    for item in result:
        if item["education"]:
            edu = item["education"].replace(" ", "").replace("\n", "")
            price = item["salary"].replace(" ", "").replace("\n", "")

            if not (re.findall("天", price)) and re.findall("(千|万)", price):
                nums = re.findall("\d+\.{0,1}\d*", price)
                flag = re.findall("(千|万)", price)
                num1 = float(nums[0]) * 1000 if flag[0] == "千" else float(nums[0]) * 10000
                num2 = float(nums[1]) * 1000 if flag[1] == "千" else float(nums[1]) * 10000
                avg = (num1 + num2) / 2
                if total.get(edu):
                    total[edu] += avg
                    totalnum[edu] += 1
                else:
                    total[edu] = avg
                    totalnum[edu] = 1

    X = []
    Y = []
    for item in total:
        X.append(item)
        Y.append(total[item] / totalnum[item])

    print(X)
    print(Y)
    return [X[:10], Y[:10]]

@app.route("/getcsScore")
def getcsScore():
    sql = "select skills from salary where type=4"
    cursor.execute(sql)
    result = cursor.fetchall()
    connection.commit()

    infos = []
    for item in result:
        infos += (item["skills"].replace(" ", "").split("\n"))

    infos = (Counter(infos))
    infos = infos.most_common()
    total = 0
    newinfos = []
    for item in infos:
        if item[0]:
            total += item[1]
            newinfos.append(list(item))
    for item in newinfos:
        item[1] = item[1] / total
    return newinfos[:10]


@app.route("/predict")
def predict():
    X = []
    city = float(request.args.get("city"))
    workage = float(request.args.get("workage"))
    education = float(request.args.get("education"))
    skills = request.args.get("skills")
    nums = 0
    for item in skills.split(","):
        nums += float(item)

    X.append(1)
    X.append(city)
    X.append(education)
    X.append(workage)
    X.append(nums)
    from theta import theta
    import numpy as np
    X = np.array(X)
    theta = np.array(theta)
    return str(X.dot(theta))


app.run()
